package com.gz.moreaseManager.modules.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gz.moreaseManager.config.SelfUserDetails;
import com.gz.moreaseManager.core.modal.AjaxResult;
import com.gz.moreaseManager.core.modal.HttpCode;
import com.gz.moreaseManager.core.modal.PageResult;
import com.gz.moreaseManager.modules.entity.Banner;
import com.gz.moreaseManager.modules.entity.MenuWeb;
import com.gz.moreaseManager.modules.service.IMenuWebService;
import com.gz.moreaseManager.utils.DateUtil;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 官网菜单表 前端控制器
 * </p>
 *
 * @author wike
 * @since 2019-02-25
 */
@RestController
@RequestMapping("/menuWeb")
public class MenuWebController {

    @Autowired
    private IMenuWebService menuWebService;

    @GetMapping("list")
    public ModelAndView list(){
        return new ModelAndView("modules/menuWeb/list");
    }

    @GetMapping("listPage")
    public PageResult<MenuWeb> findPageList(String name,@DateTimeFormat(pattern = "yyyy-MM-dd")Date startDate,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                            Integer pageNum, Integer pageSize){

        //分页
        Page paramsPage = new Page();
        paramsPage.setCurrent(pageNum);
        paramsPage.setSize(pageSize);
        //构建查询
        Wrapper wrapper = new EntityWrapper<MenuWeb>();
        if( UtilTool.isNotBlank( name ))
            wrapper.like("name",name);
        if( UtilTool.isNotBlank( startDate ) ){
            if( UtilTool.isNotBlank( endDate ) && startDate.getTime() == endDate.getTime() ){
                wrapper.between("create_date",startDate,DateUtil.addDays(startDate,1));
            }else{
                wrapper.between("create_date",startDate,
                        UtilTool.isNotBlank(endDate)?DateUtil.addDays(endDate,1):DateUtil.addDays(new Date(),1));
            }
        }
        if( UtilTool.isBlank(startDate) && UtilTool.isNotBlank( endDate )){
            wrapper.le("create_date",DateUtil.addDays(endDate,1));
        }
        //排序
        wrapper.orderBy("create_date",false);
        Page<MenuWeb> data = menuWebService.selectPage(paramsPage,wrapper);
        return new PageResult(0, null, data.getTotal(), data.getRecords());
    }

    /**
     * 新增官网菜单
     * @param menuWeb
     * @return
     */
    @PostMapping("addMenuWeb")
    public AjaxResult<Boolean> addMenuWeb(MenuWeb menuWeb,Authentication authentication){
        if( menuWeb == null
                || UtilTool.isBlank( menuWeb.getAuthCode())
                || UtilTool.isBlank( menuWeb.getName())
                || UtilTool.isBlank( menuWeb.getLink()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        menuWeb.setCreateDate( new Date() );
        //创建人
        SelfUserDetails userDetails = (SelfUserDetails) authentication.getPrincipal();
        menuWeb.setCreatePerson( userDetails.getUsername());
        Boolean success = menuWebService.insert( menuWeb );
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "新增官网菜单信息失败!");
    }

    /**
     * 修改状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("setStatus")
    public AjaxResult<Boolean> setStatus( Integer id,Integer status){
        if (UtilTool.isBlank(id ) || UtilTool.isBlank(status))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        MenuWeb menuWeb = new MenuWeb();
        menuWeb.setId( id );
        menuWeb.setStatus( status );
        Boolean success = menuWebService.updateById( menuWeb);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "修改状态失败!");
    }

    /**
     * 获取官网菜单信息
     * @param id
     * @return
     */
    @GetMapping("findMenuWeb")
    public AjaxResult<MenuWeb> findMenuWeb( Integer id ){

        if( UtilTool.isBlank( id ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        MenuWeb menuWeb = menuWebService.selectById( id );

        return menuWeb !=null ? AjaxResult.success(menuWeb):AjaxResult.build(HttpCode.FAILED, "查询官网菜单信息失败!");
    }

    @PostMapping("editMenuWeb")
    public AjaxResult<Boolean> updateMenuWeb( MenuWeb menuWeb ){
        if( menuWeb == null
                || UtilTool.isBlank( menuWeb.getId() )
                || UtilTool.isBlank( menuWeb.getAuthCode())
                || UtilTool.isBlank( menuWeb.getName())
                || UtilTool.isBlank( menuWeb.getLink()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        Boolean success = menuWebService.updateById( menuWeb );
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "修改官网菜单信息失败!");
    }

    /**
     * 获取所有的一级主菜单
     * @return
     */
    @GetMapping("findParentMenu")
    public AjaxResult<Map<Integer ,String >> findParentMenu(){

        Wrapper wrapper = new EntityWrapper<MenuWeb>();
        wrapper.eq("pid",0);
        List<MenuWeb> menuWebList = menuWebService.selectList(wrapper);

        Map<Integer ,String > parentMap = new HashMap<Integer ,String >();
        if( menuWebList !=null ){
            for(MenuWeb menuWeb : menuWebList ){
                parentMap.put(menuWeb.getId(),menuWeb.getName());
            }
        }

        return AjaxResult.success( parentMap );
    }

    /**
     * 根据权限码获取二级菜单
     * @return
     */
    @GetMapping("findChildMenu")
    public AjaxResult<List<MenuWeb>> findChildMenu( String authCode ){

        Wrapper wrapper = new EntityWrapper<MenuWeb>();
        wrapper.eq("auth_code",authCode);
        MenuWeb parentMenuWeb = menuWebService.selectOne(wrapper);

        if( parentMenuWeb !=null && UtilTool.isNotBlank( parentMenuWeb.getId())){
            Wrapper wrapper1 = new EntityWrapper<MenuWeb>();
            wrapper1.eq("pid",parentMenuWeb.getId());
            List<MenuWeb> list = menuWebService.selectList(wrapper1);
            return AjaxResult.success( list );
        }

        return AjaxResult.success( null );
    }

    @PostMapping("delete")
    public AjaxResult<Boolean> deleteMenuWeb(Integer id ){

        if( UtilTool.isBlank( id ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        Boolean success = menuWebService.deleteById( id );
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "删除官网菜单信息失败!");
    }

}
